package client

import (
	"gorm.io/gorm"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/schemas/resp"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/model/client"
	"likeadmin/util"
)

type IDramaService interface {
	All() (res []resp.DramaResp, e error)
	List(page request.PageReq, listReq req.DramaListReq) (res response.PageResp, e error)
	Detail(id string) (res resp.DramaResp, e error)
	Add(addReq req.DramaAddReq) (e error)
	Edit(editReq req.DramaEditReq) (e error)
	Del(id string) (e error)
}

// NewDramaService 初始化
func NewDramaService(db *gorm.DB) IDramaService {
	return &dramaService{db: db}
}

// dramaService 短剧服务实现类
type dramaService struct {
	db *gorm.DB
}

// All 角色所有
func (srv dramaService) All() (res []resp.DramaResp, e error) {
	var dramas []client.Drama
	err := srv.db.Order("drama_id desc").Find(&dramas).Error
	if e = response.CheckErr(err, "All Find err"); e != nil {
		return
	}
	response.Copy(&res, dramas)
	return
}

// List 短剧列表
func (srv dramaService) List(page request.PageReq, listReq req.DramaListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&client.Drama{})
	if listReq.Title != "" {
		model = model.Where("title like ?", "%"+listReq.Title+"%")
	}
	if listReq.IsCompleted >= 0 {
		model = model.Where("is_completed = ?", listReq.IsCompleted)
	}
	if listReq.Status >= 0 {
		model = model.Where("status = ?", listReq.Status)
	}

	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []client.Drama
	err = model.Limit(limit).Offset(offset).Order("drama_id desc, create_time desc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	resps := []resp.DramaResp{}
	response.Copy(&resps, objs)

	for i := range resps {
		resps[i].CoverUrl = util.UrlUtil.ToAbsoluteUrl(resps[i].CoverUrl)
	}

	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

// Detail 短剧详情
func (srv dramaService) Detail(id string) (res resp.DramaResp, e error) {
	var obj client.Drama
	err := srv.db.Where("drama_id = ?", id).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}

	obj.CoverUrl = util.UrlUtil.ToAbsoluteUrl(obj.CoverUrl)

	response.Copy(&res, obj)
	return
}

// Add 短剧新增
func (srv dramaService) Add(addReq req.DramaAddReq) (e error) {
	var obj client.Drama
	response.Copy(&obj, addReq)
	obj.CoverUrl = util.UrlUtil.ToRelativeUrl(obj.CoverUrl)
	obj.DramaId = util.UitlSnow.SnowId()
	err := srv.db.Create(&obj).Error
	e = response.CheckErr(err, "Add Create err")
	return
}

// Edit 短剧编辑
func (srv dramaService) Edit(editReq req.DramaEditReq) (e error) {
	var obj client.Drama
	err := srv.db.Where("drama_id = ?", editReq.DramaId).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}
	// 更新
	response.Copy(&obj, editReq)

	obj.CoverUrl = util.UrlUtil.ToRelativeUrl(obj.CoverUrl)
	err = srv.db.Model(&client.Drama{}).Where("drama_id = ?", editReq.DramaId).Select("title", "description", "cover_url", "is_completed", "status", "total_episodes", "money").Updates(client.Drama{
		Title:         editReq.Description,
		Description:   editReq.Description,
		CoverUrl:      editReq.CoverUrl,
		IsCompleted:   editReq.IsCompleted,
		Status:        editReq.Status,
		TotalEpisodes: editReq.TotalEpisodes,
		Money:         editReq.Money,
	}).Error

	e = response.CheckErr(err, "Edit Updates err")
	return
}

// Del 短剧删除
func (srv dramaService) Del(id string) (e error) {
	var obj client.Drama
	err := srv.db.Where("drama_id = ?", id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Del First err"); e != nil {
		return
	}
	// 删除
	err = srv.db.Delete(&obj).Error
	e = response.CheckErr(err, "Del Delete err")
	return
}
